package edu.stanford.hardon.Tabs;

import java.util.Date;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ToggleButton;
import edu.stanford.hardon.DatabaseHelper;
import edu.stanford.hardon.R;
import edu.stanford.hardon.TravelCore;
import edu.stanford.hardon.TravelHard;
import edu.stanford.hardon.TravelHardConstants;
import edu.stanford.hardon.Media.MediaType;
import edu.stanford.hardon.Media.TextMedia;
import android.content.Context;
import android.graphics.Color;
import android.location.Location;

public class NotesTab extends GenericTab{

	public NotesTab(int Id, String Text, int iconResourceId, LinearLayout tabLayout, TravelHard th) {
		super(Id, Text, iconResourceId, tabLayout, th);
		
		final Button captureButton = (Button) tabLayout.findViewById(R.id.save_note);
		captureButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	saveNoteContent();
            	captureButton.setText("Saved");
            	captureButton.setBackgroundResource(0);
            }
        });
		
		/*
		ToggleButton captureButton = (ToggleButton) tabLayout.findViewById(R.id.save_note);
		captureButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	saveNoteContent();
            }
        });
        */
	}
	


	@Override
	public void startTab() {
		Button captureButton = (Button) tabLayout.findViewById(R.id.save_note);
		captureButton.setText("Save");
    	captureButton.setBackgroundResource(R.drawable.save_button);
    	//captureButton.setBackgroundColor(Color.WHITE);
		// captureButton.setChecked(false);
		EditText noteTextField = (EditText)tabLayout.findViewById(R.id.note_text);
		noteTextField.setText("");
	}

	@Override
	public void stopTab() {
		// TODO 
		
	}

	@Override
	public void setPaused() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setResumed() {
		// TODO Auto-generated method stub
		
	}
	
	private void saveNoteContent(){
		EditText noteTextField = (EditText)tabLayout.findViewById(R.id.note_text);
		Log.i("NOTE", noteTextField.getText().toString());
		String noteText = noteTextField.getText().toString();
		Date date = new Date();
		Double lat = 0.0, lng = 0.0, alt = 0.0;
		if(th.location != null) {
			lat = th.location.getLatitude();
			lng = th.location.getLongitude();
			alt = th.location.getAltitude();
		}	
		TextMedia textMedia = new TextMedia(null, TravelHardConstants.TRIP_ID, MediaType.NOTE, lat, lng, alt, date, null, null, noteText);
		
		DatabaseHelper dbhh = TravelCore.getDBH();
		dbhh.open();
		dbhh.insert(textMedia);
		dbhh.close();
		
	}

}
